package com.petsdar.service;

import com.petsdar.dao.KeeperAccountMapper;
import com.petsdar.pojo.KeeperAccount;
import com.petsdar.utils.Constant;
import com.petsdar.utils.IDUtil;
import com.petsdar.utils.JsonUtil;
import com.petsdar.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/4
 * Time: 17:56
 * Description: keeperAccount相关的业务逻辑处理接口的实现
 * 获取店长账户业务逻辑处理；
 * 添加新店长账户业务逻辑处理；
 * 更新店长账户业务逻辑处理；
 * 删除店长账户业务逻辑处理
 * Version: V1.0
 */

@Service
public class KeeperAccountServiceImpl implements KeeperAccountService {
    /** keeperAccount 相关数据持久化操作对象 */
    private KeeperAccountMapper keeperAccountMapper;

    @Autowired
    public void setKeeperAccountMapper(KeeperAccountMapper keeperAccountMapper) {
        this.keeperAccountMapper = keeperAccountMapper;
    }

    /**
     * 获取所有店长账户信息，并返回list集合
     * @return
     */
    @Override
    public List<KeeperAccount> getKeeperAccount() {
        List<KeeperAccount> keeperAccount = keeperAccountMapper.getKeeperAccount();
        return keeperAccount;
    }

    /**
     * 添加新店长账户。将ktJson解析成keeperAccount对象，生成随机ID，
     * 对密码进行加密处理，并设置对象数据创建时间和更新时间，完善
     * keeperAccount对象。对其进行持久化操作。
     * @param ktJson 店长账户Json数据
     * @return
     */
    @Override
    public boolean addKeeperAccount(String ktJson) {
        KeeperAccount keeperAccount = JsonUtil.toObject(ktJson, KeeperAccount.class, KeeperAccountServiceImpl.class);
        Date date = new Date();
        String md5 = MD5.getMD5(keeperAccount.getKeeperAccountLoginPwd(), keeperAccount.getKeeperAccountLoginName());
        keeperAccount.setKeeperAccountId(IDUtil.getId(Constant.KEEPER_POWER_ID));
        keeperAccount.setKtCreateTime(date);
        keeperAccount.setKtUpdateTime(date);
        keeperAccount.setKeeperAccountLoginPwd(md5);
        int count = keeperAccountMapper.addKeeperAccount(keeperAccount);
        if (count == 1){
            return true;
        }
        return false;
    }

    /**
     * 通过id删除店长账户。
     * @param ktId 店长账户Id
     * @return
     */
    @Override
    public boolean delKeeperAccount(String ktId) {
        int count = keeperAccountMapper.delKeeperAccount(ktId);
        if (count == 1){
            return true;
        }
        return false;
    }

    /**
     * 更新店长账户。解析ktJson数据成keeperAccount对象。
     * 设置对象的更新时间。进行持久化操作。
     * @param ktJson 店长账户Json数据
     * @return
     */
    @Override
    public boolean updateKeeperAccount(String ktJson) {
        KeeperAccount keeperAccount = JsonUtil.toObject(ktJson, KeeperAccount.class, KeeperAccountServiceImpl.class);
        Date date = new Date();
        keeperAccount.setKtUpdateTime(date);
        int count = keeperAccountMapper.updateKeeperAccount(keeperAccount);
        if (count == 1){
            return true;
        }
        return false;
    }
}
